
#delimit;

set more off;

capture log close;


drop _all;

local t1="specify here the folder path where the data are stored";


log using `t1'sampleFRB_w1rel231_w2rel231_p, replace;
drop _all;

set mem 600m;

use `t1'w1rel231_w2rel231_ext; *Panel;
sort mergeid wave;

merge mergeid using `t1'educode2rel231;
sort mergeid wave;

*We classify individuals;
egen nwave=sum(wave), by (mergeid);
sort mergeid wave;


gen tipo=1 if nwave==1; *S�lo en 2004;
replace tipo=3 if nwave==3; *En ambas olas;
replace tipo=2 if nwave==2 ;
*drop if mergeid==mergeid[_n-1] & wave==wave[_n-1]+1;


forvalues c=11(1)20{;

           
          display "country==`c'";
         tab tipo if  country==`c';
          
         
             };
tab tipo if country==23;

tab country tipo;
*Longitudinals are duplicated;
*keep if tipo==3;*Only longitudionals;
tab wave;

gen gender1= gender;
tab gender1 if wave==1;
tab gender1 if wave==2;



*First, couples identification (id1) for recovering in one line the info about both parents as much as possible. Remember that
there are many missing spouses and therefore for them it is not possible to retrieve any info;
sort wave mergeid;
gen id=1 in 1;
replace id=id[_n-1]+1 in 2/l;

gen id1=id;
replace id1=id[_n-1] if hhid==hhid[_n-1] & cvidp==cvid[_n-1] & cvid==cvidp[_n-1]; *id1 is different for every single individual and couple;

*Only different from "." for couples;
gen couple=id1 if (hhid==hhid[_n-1] & cvidp==cvid[_n-1] & cvid==cvidp[_n-1]) | (hhid==hhid[_n+1] & cvidp==cvid[_n+1] & cvid==cvidp[_n+1]);

count if dn014==1 & couple==.;
gen missing_spouse=(dn014==1 & couple==.);
tab country missing_spouse, row;

egen hs=sum(dumfamr), by (couple); 
replace hs=dumfamr if couple==.;
tab hs;

drop if hs==. | hs==0; *No info on children (no family respondents);
drop if hs==2; *Both are family respondents, mistakes;


**We build the characteristics of the parents;
drop phealth;
gen phealth=sphus if wave==1;
replace phealth=ph003_ if wave==2;
gen phealthm=sphus if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0) & wave==1 ; 
replace phealthm=sphus if ((id1==id1[_n-1]) & dumfamr==1 & hs==2)  & wave==1;
replace phealthm=ph003_ if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0) & wave==2 ;
replace phealthm=ph003_ if ((id1==id1[_n-1]) & dumfamr==1 & hs==2)  & wave==2; 
sort id1;
egen phealthm1=sum(phealthm), by (id1);
sort id1;
drop phealthm;
rename phealthm1 phealthm; 

gen ph003m=ph003_ if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0)  ; 
sort id1;
egen ph003m1=sum(ph003m), by (id1);
sort id1;
drop ph003m;
rename ph003m1 ph003m; 


gen ph049d1m=ph049d1 if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0)  ; 
sort id1;
egen ph049d1m1=sum(ph049d1m), by (id1);
sort id1;
drop ph049d1m;
rename ph049d1m1 ph049d1m; 

gen ph010d1m=ph010d1 if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0)  ; 
sort id1;
egen ph010d1m1=sum(ph010d1m), by (id1);
sort id1;
drop ph010d1m;
rename ph010d1m1 ph010d1m; 


gen ph006d1m=ph006d1 if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0)  ; 
sort id1;
egen ph006d1m1=sum(ph006d1m), by (id1);
sort id1;
drop ph006d1m;
rename ph006d1m1 ph006d1m; 


gen ph048d1m=ph048d1 if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0)  ; 
sort id1;
egen ph048d1m1=sum(ph048d1m), by (id1);
sort id1;
drop ph048d1m;
rename ph048d1m1 ph048d1m; 


gen ph005m=ph005_ if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0)  ; 
sort id1;
egen ph005m1=sum(ph005m), by (id1);
sort id1;
drop ph005m;
rename ph005m1 ph005m; 



gen hc029m=hc029_ if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0)  ; 
sort id1;
egen hc029m1=sum(hc029m), by (id1);
sort id1;
drop hc029m;
rename hc029m1 hc029m; 

gen hc032d1m=hc032d1 if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0)  ; 
sort id1;
egen hc032d1m1=sum(hc032d1m), by (id1);
sort id1;
drop hc032d1m;
rename hc032d1m1 hc032d1m; 


gen sp021d1m=sp021d1 if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0)  ; 
sort id1;
egen sp021d1m1=sum(sp021d1m), by (id1);
sort id1;
drop sp021d1m;
rename sp021d1m1 sp021d1m; 

gen sp020m=sp020_ if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0)  ; 
sort id1;
egen sp020m1=sum(sp020m), by (id1);
sort id1;
drop sp020m;
rename sp020m1 sp020m; 

gen hc039d9m=hc039d9 if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0)  ; 
sort id1;
egen hc039d9m1=sum(hc039d9m), by (id1);
sort id1;
drop hc039d9m;
rename hc039d9m1 hc039d9m; 

gen hc039d10m=hc039d10 if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0)  ; 
sort id1;
egen hc039d10m1=sum(hc039d10m), by (id1);
sort id1;
drop hc039d10m;
rename hc039d10m1 hc039d10m; 

gen hc039d11m=hc039d11 if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0)  ; 
sort id1;
egen hc039d11m1=sum(hc039d11m), by (id1);
sort id1;
drop hc039d11m;
rename hc039d11m1 hc039d11m; 


gen adlg=adl if wave==1;
replace adlg=(ph049d1+ph049d2+ph049d3+ph049d4+ph049d5+ph049d6) if wave==2;

gen iadlg=iadl if wave==1;
replace iadlg=(ph049d7+ph049d8+ph049d9+ph049d10+ph049d11+ph049d12+ph049d13) if wave==2;

gen adlg_k=(ph049d1+ph010d11+ph049d3+ph049d4+ph049d5+ph049d6);

gen mobilityg=mobility if wave==1;
replace mobilityg=(ph048d1+ph048d2+ph048d3+ph048d4+ph048d5+ph048d6+ph048d7+ph048d8+ph048d9+ph048d10) if wave==2;

gen adlgm=adlg if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0)  ; 
replace adlgm=adlg if ((id1==id1[_n-1]) & dumfamr==1 & hs==2) ; 
sort id1;
egen adlgm1=sum(adlgm), by (id1);
sort id1;
drop adlgm;
rename adlgm1 adlgm; 


gen adlg_km=adlg_k if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0)  ; 
replace adlg_km=adlg_k if ((id1==id1[_n-1]) & dumfamr==1 & hs==2) ; 
sort id1;
egen adlg_km1=sum(adlg_km), by (id1);
sort id1;
drop adlg_km;
rename adlg_km1 adlg_km; 

gen iadlgm=iadlg if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0)  ; 
replace iadlgm=iadlg if ((id1==id1[_n-1]) & dumfamr==1 & hs==2) ; 
sort id1;
egen iadlgm1=sum(iadlgm), by (id1);
sort id1;
drop iadlgm;
rename iadlgm1 iadlgm; 


gen mobilitygm=mobilityg if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0)  ; 
replace mobilitygm=mobilityg if ((id1==id1[_n-1]) & dumfamr==1 & hs==2) ; 
sort id1;
egen mobilitygm1=sum(mobilitygm), by (id1);
sort id1;
drop mobilitygm;
rename mobilitygm1 mobilitygm; 

 
gen limited=ph005_ ;
gen limitedm=ph005_ if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0)  ; 
replace limitedm=ph005_ if ((id1==id1[_n-1]) & dumfamr==1 & hs==2) ; 
sort id1;
egen limitedm1=sum(limited), by (id1);
sort id1;
drop limitedm;
rename limitedm1 limitedm; 


gen nurs=(hc029_==1 | hc029_==3);
gen nursm=nurs if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0) ; 
sort  id1;
egen nursm1=sum(nursm), by (id1);
sort  id1;
drop nursm;
rename nursm1 nursm; 

gen tnurs=hc030_;
gen tnursm=tnurs if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0) ; 
sort  id1;
egen tnursm1=sum(tnursm), by (id1);
sort  id1;
drop tnursm;
rename tnursm1 tnursm; 

gen wnurs=hc031_;
gen wnursm=wnurs if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0) ; 
sort  id1;
egen wnursm1=sum(wnursm), by (id1);
sort  id1;
drop wnursm;
rename wnursm1 wnursm; 

gen homca1=(hc032d1==1);
gen homca1m=homca1 if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0) ; 
sort  id1;
egen homca1m1=sum(homca1m), by (id1);
sort  id1;
drop homca1m;
rename homca1m1 homca1m; 

gen homca2=(hc032d2==1);
gen homca2m=homca2 if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0) ; 
sort  id1;
egen homca2m1=sum(homca2m), by (id1);
sort  id1;
drop homca2m;
rename homca2m1 homca2m; 

gen homca3=(hc032d3==1);
gen homca3m=homca3 if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0) ; 
sort  id1;
egen homca3m1=sum(homca3m), by (id1);
sort  id1;
drop homca3m;
rename homca3m1 homca3m; 

drop chronic chronic2;
gen chronic=(ph006d1+ ph006d4+ ph006d6+ ph006d8 + ph006d10+ ph006d12+ ph006d14);
gen mental=(ph006d16==1);
gen chronicm=chronic if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0) ; 
gen mentalm=mental if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0) ; 
sort  id1;
egen chronicm1=sum(chronicm), by (id1);
sort  id1;
egen mentalm1=sum(mentalm), by (id1);
sort  id1;

drop chronicm;
rename chronicm1 chronicm; 
drop mentalm;
rename mentalm1 mentalm; 

sort id1;
gen helpo=(sp002==1) if dumfamr==1; *Care received from outside the HH. This is just reported by the family respondent;
gen helpi=(sp020_==1); *Care received from inside the HH. This is answered from everybody that reported to have any condition on ph048;
gen helpim=helpi if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0)  ; 
replace helpim=helpi if ((id1==id1[_n-1]) & dumfamr==1 & hs==2) ; 
sort id1;
egen helpim1=sum(helpim), by (id1);
sort id1;
drop helpim;
rename helpim1 helpim; 

*Dummy variables that state whether child1-child9 have taking care of one respondent parent;
*We identify whether each child is taking care of parents;

gen helpo1=1 if sp003_1==10;  *Care from a person from outside the household. Only for the FR;
replace helpo1=2 if sp003_1==11;
replace helpo1=3 if sp003_1==12;
replace helpo1=4 if sp003_1==13;
replace helpo1=5 if sp003_1==14;
replace helpo1=6 if sp003_1==15;
replace helpo1=7 if sp003_1==16;
replace helpo1=8 if sp003_1==17;
replace helpo1=9 if sp003_1==18;

gen helpo2=1 if sp003_2==10;
replace helpo2=2 if sp003_2==11;
replace helpo2=3 if sp003_2==12;
replace helpo2=4 if sp003_2==13;
replace helpo2=5 if sp003_2==14;
replace helpo2=6 if sp003_2==15;
replace helpo2=7 if sp003_2==16;
replace helpo2=8 if sp003_2==17;
replace helpo2=9 if sp003_2==18;

gen helpo3=1 if sp003_3==10;
replace helpo3=2 if sp003_3==11;
replace helpo3=3 if sp003_3==12;
replace helpo3=4 if sp003_3==13;
replace helpo3=5 if sp003_3==14;
replace helpo3=6 if sp003_3==15;
replace helpo3=7 if sp003_3==16;
replace helpo3=8 if sp003_3==17;
replace helpo3=9 if sp003_3==18;



       
gen helpi1=(sp021d10==1);
gen helpi2=(sp021d11==1);
gen helpi3=(sp021d12==1);
gen helpi4=(sp021d13==1);
gen helpi5=(sp021d14==1);
gen helpi6=(sp021d15==1);
gen helpi7=(sp021d16==1);
gen helpi8=(sp021d17==1);
gen helpi9=(sp021d18==1);



       forv k=1(1)9{;
gen helpi`k'm=helpi`k' if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0)  ; 
replace helpi`k'm=helpi`k' if ((id1==id1[_n-1]) & dumfamr==1 & hs==2) ; 
sort id1;
egen helpi`k'm1=sum(helpi`k'm), by (id1);
sort id1;
drop helpi`k'm;
rename helpi`k'm1 helpi`k'm; 
                            };

*We compute whether one spouse have received care from other people different from children;
gen ocare_out=(((sp003_1==1 | sp003_1==2 | sp003_1==3 | sp003_1==4 | sp003_1==5 | sp003_1==6 | sp003_1==7 | sp003_1==8 | sp003_1==9 | sp003_1==19 |
sp003_1==20 | sp003_1==21 | sp003_1==22 |  sp003_1==23 | sp003_1==24 | sp003_1==25 | sp003_1==26 | sp003_1==27 | sp003_1==28 | sp003_1==29 | sp003_1==30 |
sp003_1==31 | sp003_1==32 | sp003_1==33) & sp005_1==1) |
((sp003_2==1 | sp003_2==2 | sp003_2==3 | sp003_2==4 | sp003_2==5 | sp003_2==6 | sp003_2==7 | sp003_2==8 | sp003_2==9 | sp003_2==19 |
sp003_2==20 | sp003_2==21 | sp003_2==22 |  sp003_2==23 | sp003_2==24 | sp003_2==25 | sp003_2==26 | sp003_2==27 | sp003_2==28 | sp003_2==29 | sp003_2==30 |
sp003_2==31 | sp003_2==32 | sp003_2==33) & sp005_2==1) |
((sp003_3==1 | sp003_3==2 | sp003_3==3 | sp003_3==4 | sp003_3==5 | sp003_3==6 | sp003_3==7 | sp003_3==8 | sp003_3==9 | sp003_3==19 |
sp003_3==20 | sp003_3==21 | sp003_3==22 |  sp003_3==23 | sp003_3==24 | sp003_3==25 | sp003_3==26 | sp003_3==27 | sp003_3==28 | sp003_3==29 | sp003_3==30 |
sp003_3==31 | sp003_3==32 | sp003_3==33) & sp005_3==1));


gen ocare_in=(sp021d1==1 | sp021d2==1 | sp021d3==1 | sp021d4==1 | sp021d5==1 | sp021d6==1 | sp021d7==1 | sp021d8==1 | sp021d9==1 | sp021d19==1 |
sp021d20==1 | sp021d21==1 | sp021d22==1 | sp021d23==1 | sp021d24==1 | sp021d25==1 | sp021d26==1 | sp021d27==1 | sp021d28==1 | sp021d29==1 | sp021d30==1 |
sp021d31==1 | sp021d32==1 | sp021d33==1);

gen ocare=(ocare_out==1 | ocare_in==1);
  
gen ocarem=ocare if ((id1==id1[_n-1] | id1==id1[_n+1]) & dumfamr==0)  ; 
replace ocarem=ocare if ((id1==id1[_n-1]) & dumfamr==1 & hs==2) ; 
sort id1;
egen ocarem1=sum(ocarem), by (id1);
sort id1;
drop ocarem;
rename ocarem1 ocarem; 

gen ocare2=(ocare==1 | ocarem==1);

**We drop those spouses at the couple level who are not FR;
count if couple!=.;
drop if couple!=. & dumfamr==0;

**Children characteristics;
**Drop those who do not have children;
drop if ch001==0;
drop if sp002==-1 | sp002==-2;
drop if sp003_1==-1 | sp003_1==-2;
drop if sp003_2==-1 | sp003_2==-2;
drop if sp003_3==-1 | sp003_3==-2;
drop if sp005_1==-1 | sp005_1==-2;
drop if sp005_2==-1 | sp005_2==-2;
drop if sp005_3==-1 | sp005_3==-2;
drop if sp020_==-1 | sp020_==-2;
drop if (sp020m==-1 | sp020m==-2) & couple!=. ;
drop if sp021d1==-1 | sp021d1==-2;
drop if (sp021d1m==-1 | sp021d1m==-2) & couple!=.;
drop if hc029_==-1 | hc029_==-2;
drop if (hc029m==-1 | hc029m==-2) & couple!=.;
drop if (hc032d1==-1 | hc032d1==-2);
drop if (hc032d1m==-1 | hc032d1m==-2) & couple!=.;
drop if ph005_==-1 | ph005_==-2;
drop if (ph005m==-1 | ph005m==-2) & couple!=. ;
drop if ph003_==-1 | ph003_==-2;
drop if (ph003m==-1 | ph003m==-2) & couple!=. ;
drop if ph049d1==-1 | ph049d1==-2;
drop if (ph049d1m==-1 | ph049d1m==-2) & couple!=.;
drop if ph006d1==-1 | ph006d1==-2;
drop if (ph006d1m==-1 | ph006d1m==-2) & couple!=.;
drop if ph010d1==-1 | ph010d2==-2;
drop if (ph010d1m==-1 | ph010d1m==-2) & couple!=.;

*drop if ph048ddk==1 | ph048drf==1;
*drop if (ph048ddkm==1 | ph048drfm==1) & couple!=.;
drop if ph048d1==.;
drop if (ph048d1m==.) & couple!=.;
drop if ch001==.;
drop if ch001==-1 | ch001==-2;

forv g=1(1)16{;

	  drop if ch005_`g'==-1 | ch005_`g'==-2; 	
        gen son`g'=(ch005_`g'==1) ;
        gen daugt`g'=(ch005_`g'==2);
        

                     }; 

gen sons=son1+son2+son3+son4+son5+son6+son7+son8+son9+son10+son11+son12+son13+son14+son15+son16;
gen daugts=daugt1+daugt2+daugt3+daugt4+daugt5+daugt6+daugt7+daugt8+daugt9+daugt10+daugt11+daugt12+daugt13+daugt14+daugt15+daugt16;

drop son1-son16;
drop daugt1-daugt16;

*Gender, age and residence identification for the four selected children;

forv g=1(1)16{;
             

             drop if (ch005_`g'==-1 | ch005_`g'==-2) & chselch1==`g'; 
             drop if (ch006_`g'==-1 | ch006_`g'==-2) & chselch1==`g'; 
             gen sex1`g'=ch005_`g' if chselch1==`g' ;
             replace sex1`g'=0 if chselch1!=`g' ;
             gen age1`g'=2004-ch006_`g' if chselch1==`g' & wave==1;
             replace age1`g'=int_year_w2-ch006_`g' if chselch1==`g' & wave==2;
             replace age1`g'=0 if chselch1!=`g' ;
		 gen yearb1`g'=ch006_`g' if chselch1==`g' & wave==1;
             replace yearb1`g'=ch006_`g' if chselch1==`g' & wave==2;
             replace yearb1`g'=0 if chselch1!=`g' ;
             
             gen resi1`g'=ch007_`g' if chselch1==`g' & wave==1 ;
             replace resi1`g'=ch007_`g' if chselch1==`g' & wave==2 ;
		 replace resi1`g'=0 if chselch1!=`g' ;



		 drop if (ch005_`g'==-1 | ch005_`g'==-2) & chselch2==`g'; 
             drop if (ch006_`g'==-1 | ch006_`g'==-2) & chselch2==`g'; 
             gen sex2`g'=ch005_`g' if chselch2==`g' ;
             replace sex2`g'=0 if chselch2!=`g' ;
             gen age2`g'=2004-ch006_`g' if chselch2==`g' & wave==1;
             replace age2`g'=int_year_w2-ch006_`g' if chselch2==`g' & wave==2;
             replace age2`g'=0 if chselch2!=`g' ;
		 gen yearb2`g'=ch006_`g' if chselch2==`g' & wave==1;
             replace yearb2`g'=ch006_`g' if chselch2==`g' & wave==2;
             replace yearb2`g'=0 if chselch2!=`g' ;
             gen resi2`g'=ch007_`g' if chselch2==`g' & wave==1 ;
             replace resi2`g'=ch007_`g' if chselch2==`g' & wave==2 ;
		 replace resi2`g'=0 if chselch2!=`g' ;
             
		 drop if (ch005_`g'==-1 | ch005_`g'==-2) & chselch3==`g'; 
             drop if (ch006_`g'==-1 | ch006_`g'==-2) & chselch3==`g'; 
             gen sex3`g'=ch005_`g' if chselch3==`g' ;
             replace sex3`g'=0 if chselch3!=`g' ;
             gen age3`g'=2004-ch006_`g' if chselch3==`g' & wave==1;
             replace age3`g'=int_year_w2-ch006_`g' if chselch3==`g' & wave==2;
             replace age3`g'=0 if chselch3!=`g' ;
		 gen yearb3`g'=ch006_`g' if chselch3==`g' & wave==1;
             replace yearb3`g'=ch006_`g' if chselch3==`g' & wave==2;
             replace yearb3`g'=0 if chselch3!=`g' ;
             gen resi3`g'=ch007_`g' if chselch3==`g' & wave==1 ;
             replace resi3`g'=ch007_`g' if chselch3==`g' & wave==2 ;
		 replace resi3`g'=0 if chselch3!=`g' ;


             
		 drop if (ch005_`g'==-1 | ch005_`g'==-2) & chselch4==`g'; 
             drop if (ch006_`g'==-1 | ch006_`g'==-2) & chselch4==`g'; 
		 gen sex4`g'=ch005_`g' if chselch4==`g' ;
             replace sex4`g'=0 if chselch4!=`g' ;
             gen age4`g'=2004-ch006_`g' if chselch4==`g' & wave==1;
             replace age4`g'=int_year_w2-ch006_`g' if chselch4==`g' & wave==2;
             replace age4`g'=0 if chselch4!=`g' ;
		 gen yearb4`g'=ch006_`g' if chselch4==`g' & wave==1;
             replace yearb4`g'=ch006_`g' if chselch4==`g' & wave==2;
             replace yearb4`g'=0 if chselch4!=`g' ;
             gen resi4`g'=ch007_`g' if chselch4==`g' & wave==1 ;
             replace resi4`g'=ch007_`g' if chselch4==`g' & wave==2 ;
		 replace resi4`g'=0 if chselch4!=`g' ;

                     }; 

gen sex1=sex11+sex12+sex13+sex14+sex15+sex16+sex17+sex18+sex19+sex110+sex111+sex112+sex113+sex114+sex115+sex116;
gen sex2=sex21+sex22+sex23+sex24+sex25+sex26+sex27+sex28+sex29+sex210+sex211+sex212+sex213+sex214+sex215+sex216;
gen sex3=sex31+sex32+sex33+sex34+sex35+sex36+sex37+sex38+sex39+sex310+sex311+sex312+sex313+sex314+sex315+sex316;
gen sex4=sex41+sex42+sex43+sex44+sex45+sex46+sex47+sex48+sex49+sex410+sex411+sex412+sex413+sex414+sex415+sex416;

gen age1=age11+age12+age13+age14+age15+age16+age17+age18+age19+age110+age111+age112+age113+age114+age115+age116;
gen age2=age21+age22+age23+age24+age25+age26+age27+age28+age29+age210+age211+age212+age213+age214+age215+age216;
gen age3=age31+age32+age33+age34+age35+age36+age37+age38+age39+age310+age311+age312+age313+age314+age315+age316;
gen age4=age41+age42+age43+age44+age45+age46+age47+age48+age49+age410+age411+age412+age413+age414+age415+age416;

gen yearb1=yearb11+yearb12+yearb13+yearb14+yearb15+yearb16+yearb17+yearb18+yearb19+yearb110+yearb111+yearb112+yearb113+yearb114+yearb115+yearb116;
gen yearb2=yearb21+yearb22+yearb23+yearb24+yearb25+yearb26+yearb27+yearb28+yearb29+yearb210+yearb211+yearb212+yearb213+yearb214+yearb215+yearb216;
gen yearb3=yearb31+yearb32+yearb33+yearb34+yearb35+yearb36+yearb37+yearb38+yearb39+yearb310+yearb311+yearb312+yearb313+yearb314+yearb315+yearb316;
gen yearb4=yearb41+yearb42+yearb43+yearb44+yearb45+yearb46+yearb47+yearb48+yearb49+yearb410+yearb411+yearb412+yearb413+yearb414+yearb415+yearb416;

gen resi1=resi11+resi12+resi13+resi14+resi15+resi16+resi17+resi18+resi19+resi110+resi111+resi112+resi113+resi114+resi115+resi116;
gen resi2=resi21+resi22+resi23+resi24+resi25+resi26+resi27+resi28+resi29+resi210+resi211+resi212+resi213+resi214+resi215+resi216;
gen resi3=resi31+resi32+resi33+resi34+resi35+resi36+resi37+resi38+resi39+resi310+resi311+resi312+resi313+resi314+resi315+resi316;
gen resi4=resi41+resi42+resi43+resi44+resi45+resi46+resi47+resi48+resi49+resi410+resi411+resi412+resi413+resi414+resi415+resi416;


drop sex11-sex116;
drop age11-age116;
drop yearb11-yearb116;
drop resi11-resi116; 
drop if sex1==. | sex2==. | sex3==. | sex4==.;
drop if age1==. | age2==. | age3==. | age4==.;
drop if yearb1==. | yearb2==. | yearb3==. | yearb4==.;




*We identify whether each child cares or not;
*From outside the household;

       forv k=1(1)3{;


             gen carero1`k'=(helpo`k'==chselch1) ;
             gen intens1`k'=sp005_`k' if (helpo`k'==chselch1) ;
             replace intens1`k'=0 if intens1`k'==.;
             
             gen carero2`k'=(helpo`k'==chselch2) ;
             gen intens2`k'=sp005_`k'  if (helpo`k'==chselch2);
             replace intens2`k'=0 if intens2`k'==.;
             
             gen carero3`k'=(helpo`k'==chselch3) ;
             gen intens3`k'=sp005_`k' if (helpo`k'==chselch3);
             replace intens3`k'=0 if intens3`k'==.;

             gen carero4`k'=(helpo`k'==chselch4) ;
             gen intens4`k'=sp005_`k'  if (helpo`k'==chselch4) ;
             replace intens4`k'=0 if intens4`k'==.;
            

                            }; 


gen carero1=carero11+carero12+carero13; *Aqu� hay 9 que son citados dos veces y tienen carero1==2;
gen carero2=carero21+carero22+carero23; *Aqu� hay 9 que son citados dos veces y tienen carero2==2;
gen carero3=carero31+carero32+carero33; *Aqu� hay 3 que son citados dos veces y tienen carero3==2;
gen carero4=carero41+carero42+carero43;

gen ocarero1=((helpo1!=chselch1 & helpo1!=.) | (helpo2!=chselch1 & helpo2!=.) | (helpo3!=chselch1 & helpo3!=.));
gen ocarero2=((helpo1!=chselch2 & helpo1!=.) | (helpo2!=chselch2 & helpo2!=.) | (helpo3!=chselch2 & helpo3!=.));
gen ocarero3=((helpo1!=chselch3 & helpo1!=.) | (helpo2!=chselch3 & helpo2!=.) | (helpo3!=chselch3 & helpo3!=.));
gen ocarero4=((helpo1!=chselch4 & helpo1!=.) | (helpo2!=chselch4 & helpo2!=.) | (helpo3!=chselch4 & helpo3!=.));

replace ocarero1=0 if chselch1==0;
replace ocarero2=0 if chselch2==0;
replace ocarero3=0 if chselch3==0;
replace ocarero4=0 if chselch4==0;



drop if chselch1>9 & (sp003_1==19 | sp003_2==19 | sp003_3==19); *We do not know whether they are or not;
drop if chselch2>9 & (sp003_1==19 | sp003_2==19 | sp003_3==19);
drop if chselch3>9 & (sp003_1==19 | sp003_2==19 | sp003_3==19);
drop if chselch4>9 & (sp003_1==19 | sp003_2==19 | sp003_3==19);
drop if carero1==2 | carero2==2 | carero3==2 | carero4==2;


gen intens1=intens11+intens12+intens13;
gen intens2=intens21+intens22+intens23;
gen intens3=intens31+intens32+intens33;
gen intens4=intens41+intens42+intens43;


**From inside the household;
*From inside the household; *Here there is not intensity;

*for the first person in couples;

       forv k=1(1)9{;

             gen careri1`k'=(helpi`k'==1 & chselch1==`k') ; 
             gen careri2`k'=(helpi`k'==1 & chselch2==`k') ;
             gen careri3`k'=(helpi`k'==1 & chselch3==`k') ;
             gen careri4`k'=(helpi`k'==1 & chselch4==`k') ;


                     }; 
gen careri1=careri11+careri12+careri13+careri14+careri15+careri16+careri17+careri18+careri19;
gen careri2=careri21+careri22+careri23+careri24+careri25+careri26+careri27+careri28+careri29;
gen careri3=careri31+careri32+careri33+careri34+careri35+careri36+careri37+careri38+careri39;
gen careri4=careri41+careri42+careri43+careri44+careri45+careri46+careri47+careri48+careri49;

drop helpi helpim;
gen helpi=1 if helpi1==1;
replace helpi=2 if helpi2==1;
replace helpi=3 if helpi3==1;
replace helpi=4 if helpi4==1;
replace helpi=5 if helpi5==1;
replace helpi=6 if helpi6==1;
replace helpi=7 if helpi7==1;
replace helpi=8 if helpi8==1;
replace helpi=9 if helpi9==1;

gen helpim=1 if helpi1m==1;
replace helpim=2 if helpi2m==1;
replace helpim=3 if helpi3m==1;
replace helpim=4 if helpi4m==1;
replace helpim=5 if helpi5m==1;
replace helpim=6 if helpi6m==1;
replace helpim=7 if helpi7m==1;
replace helpim=8 if helpi8m==1;
replace helpim=9 if helpi9m==1;
                    

gen ocareri1=((helpi!=chselch1 & helpi!=.) | (helpim!=chselch1 & helpim!=.));
gen ocareri2=((helpi!=chselch2 & helpi!=.) | (helpim!=chselch2 & helpim!=.));
gen ocareri3=((helpi!=chselch3 & helpi!=.) | (helpim!=chselch3 & helpim!=.));
gen ocareri4=((helpi!=chselch4 & helpi!=.) | (helpim!=chselch4 & helpim!=.));

replace ocareri1=0 if chselch1==0;
replace ocareri2=0 if chselch2==0;
replace ocareri3=0 if chselch3==0;
replace ocareri4=0 if chselch4==0;

*for the second person in couples;

       forv k=1(1)9{;


             gen careri1`k'm=(helpi`k'm==1 & chselch1==`k') ; 
             gen careri2`k'm=(helpi`k'm==1 & chselch2==`k') ;
             gen careri3`k'm=(helpi`k'm==1 & chselch3==`k') ;
             gen careri4`k'm=(helpi`k'm==1 & chselch4==`k') ;


                     }; 
gen careri1m=careri11m+careri12m+careri13m+careri14m+careri15m+careri16m+careri17m+careri18m+careri19m;
gen careri2m=careri21m+careri22m+careri23m+careri24m+careri25m+careri26m+careri27m+careri28m+careri29m;
gen careri3m=careri31m+careri32m+careri33m+careri34m+careri35m+careri36m+careri37m+careri38m+careri39m;
gen careri4m=careri41m+careri42m+careri43m+careri44m+careri45m+careri46m+careri47m+careri48m+careri49m;

*We identify observations with daughters between 50-60 years old;

gen sel1=(sex1==2 & age1>=50 & age1<=60); 
gen sel2=(sex2==2 & age2>=50 & age2<=60);
gen sel3=(sex3==2 & age3>=50 & age3<=60);
gen sel4=(sex4==2 & age4>=50 & age4<=60);

gen sel=sel1+sel2+sel3+sel4;

drop nwave;
sort mergeid wave;
*We now classify the individuals;
egen nwave=sum(wave), by (mergeid);
sort mergeid wave;



keep if wave==2;
tab ch001;
gen ch1_4=(ch001<=4);
tab ch1_4;
tab sel;
drop if sel==0;
sort mergeid wave;


expand 4; *We create one row for each children;

*Sons/Daughters identification;
sort mergeid wave;

gen idsist=1 in 1;
replace idsist=1 if id1!=id1[_n-1];
replace idsist=idsist[_n-1]+1 if id1==id1[_n-1];

gen genderh=sex1 if idsist==1; *Gender of children;
replace genderh=sex2 if idsist==2;
replace genderh=sex3 if idsist==3;
replace genderh=sex4 if idsist==4;

drop age;
gen age=age1 if idsist==1;
replace age=age2 if idsist==2;
replace age=age3 if idsist==3;
replace age=age4 if idsist==4;

drop age1;
gen age1=age/10;
gen age12=age^2;

gen resi=resi1 if idsist==1;
replace resi=resi2 if idsist==2;
replace resi=resi3 if idsist==3;
replace resi=resi4 if idsist==4;

gen resident=(resi==1);
gen resident1=(resi<=4);
gen resident2=(resi==2 | resi==3 | resi==4);




gen yearb=yearb1 if idsist==1;
replace yearb=yearb2 if idsist==2;
replace yearb=yearb3 if idsist==3;
replace yearb=yearb4 if idsist==4;

gen carero=carero1 if idsist==1;
replace carero=carero2 if idsist==2;
replace carero=carero3 if idsist==3;
replace carero=carero4 if idsist==4;

gen intens=intens1 if idsist==1;
replace intens=intens2 if idsist==2;
replace intens=intens3 if idsist==3;
replace intens=intens4 if idsist==4;

gen careri=((careri1==1 | careri1m==1)) if idsist==1;
replace careri=((careri2==1 | careri2m==1)) if  idsist==2;
replace careri=((careri3==1 | careri3m==1)) if  idsist==3;
replace careri=((careri4==1 | careri4m==1)) if  idsist==4;

gen carer=(carero>0 | careri==1);
gen atweek1=(careri==1 | (carero>0 & (intens==1 | intens==2)));
gen daily=(careri==1 | (carero>0 & intens==1));

drop ocare;
**the dummy variable tells us whether at least one spouse in the couple has been taken care of by someone different from a child (ocare2) or
by a child different from the selected daughter (ocarero and ocareri);
gen ocare=(ocarero1==1 | ocareri1==1 | ocare2==1) if idsist==1;
replace ocare=(ocarero2==1 | ocareri2==1 | ocare2==1) if idsist==2;
replace ocare=(ocarero3==1 | ocareri3==1 | ocare2==1) if idsist==3;
replace ocare=(ocarero4==1 | ocareri4==1 | ocare2==1) if idsist==4;


gen both=(couple!=. | (couple==. & (dn014==1 | dn014==2)) );
gen parent=(couple==. & gender==1 & (dn014!=1 & dn014!=2));
gen mother=(couple==. & gender==2 & (dn014!=1 & dn014!=2));

drop phealth2;
gen phealth2=phealth if couple==.;
replace phealth2=phealth if couple!=. & phealth>=phealthm;
replace phealth2=phealthm if couple!=. & phealth<=phealthm;

gen adlg2=adlg if couple==.;
replace adlg2=adlg if couple!=. & adlg>=adlgm;
replace adlg2=adlgm if couple!=. & adlg<=adlgm;

gen iadlg2=iadlg if couple==.;
replace iadlg2=iadlg if couple!=. & iadlg>=iadlgm;
replace iadlg2=iadlgm if couple!=. & iadlg<=iadlgm;

gen adlg_k2=adlg_k if couple==.;
replace adlg_k2=adlg_k if couple!=. & adlg_k>=adlg_km;
replace adlg_k2=adlg_km if couple!=. & adlg_k<=adlg_km;

gen mobilityg2=mobilityg if couple==.;
replace mobilityg2=mobilityg if couple!=. & mobilityg>=mobilitygm;
replace mobilityg2=mobilitygm if couple!=. & mobilityg<=mobilitygm;

gen limitedg2=limited if couple==.;
replace limitedg2=limited if couple!=. & limited>=limitedm;
replace limitedg2=limitedm if couple!=. & limited<=limitedm;

gen chronic2=chronic if couple==.;
replace chronic2=chronic if couple!=. & chronic>=chronicm;
replace chronic2=chronicm if couple!=. & chronic<=chronicm;

gen mental2=mental if couple==.;
replace mental2=mental if couple!=. & mental>=mentalm;
replace mental2=mentalm if couple!=. & mental<=mentalm;

gen occupat=ch016_1 if idsist==1;
replace occupat=ch016_2 if idsist==2;
replace occupat=ch016_3 if idsist==3;
replace occupat=ch016_4 if idsist==4; 
drop if occupat==-1 | occupat==-2;
gen worker1=(occupat==1 | occupat==2 | occupat==3);
drop if occupat==8 | occupat==7;

sort mergeid idsist wave;
gen id2=1 in 1;
replace id2=id2[_n-1]+1 in 2/l;



keep if genderh==2;
keep if age>=50 & age<=60;


gen phealthvg=(phealth2==1 | phealth2==2);
gen phealthg=(phealth2==3);
gen phealthf=(phealth2==4);
gen phealthb=(phealth2==5);
gen ivphealth=(phealthb==1);
gen ivphealth2=(phealth2==5 | phealth2==4);


**He cambiado estas definiciones el 13 de agosto para probar lo del vector de instrumentos a 0;
gen adlgd=(adlg2==1);
*gen adlgd2=(adlg2>=3);
gen adlgd2=(adlg2>=1);
gen adlgd3=(adlg2==6);
gen mobilitygd=(mobilityg2>=3);
*gen mobilitygd=(mobilityg2>=1);
gen mobilitygd2=(mobilityg2>=6);
gen limitedgd=(limitedg2==1);
*gen chronicgd=(chronic2>=3);
gen chronicgd=(chronic2>=1);
gen mentald=(mental2==1);



gen eduh=edu1 if idsist==1;
replace eduh=edu2 if idsist==2;
replace eduh=edu3 if idsist==3;
replace eduh=edu4 if idsist==4; 



drop if eduh==. | eduh==97;

gen educ1=(eduh==0 | eduh==1 | eduh==95);
gen educ2=(eduh==2);
gen educ3=(eduh==3);
gen educ4=(eduh==5 | eduh==6 | eduh==4 | eduh==8);

gen marital=ch012_1 if idsist==1;
replace marital=ch012_2 if idsist==2;
replace marital=ch012_3 if idsist==3;
replace marital=ch012_4 if idsist==4;

drop if marital==-2 | marital==-1;

gen married=(marital==1 | marital==2);

gen children=ch019_1 if idsist==1;
replace children=ch019_2 if idsist==2;
replace children=ch019_3 if idsist==3;
replace children=ch019_4 if idsist==4; 
drop if ch019_1==-1 | ch019_1==-2;
drop if ch019_2==-1 | ch019_2==-2;
drop if ch019_3==-1 | ch019_3==-2;
drop if ch019_4==-1 | ch019_4==-2;


gen ychildren=int_year_w2-ch020_1 if idsist==1 & children>0;
replace ychildren=int_year_w2-ch020_2 if idsist==2 & children>0;
replace ychildren=int_year_w2-ch020_3 if idsist==3 & children>0;
replace ychildren=int_year_w2-ch020_4 if idsist==4 & children>0; 



gen dchild16=(ychildren>0 & ychildren<=16);
gen nchild2=children;
gen brothers2=sons if genderh==2;
gen sisters2=daugts-1 if genderh==2;

gen sisters=sisters2;
gen brothers=brothers2;

gen austria=(country==11);
gen germany=(country==12);
gen sweden=(country==13);
gen netherlands=(country==14);
gen spain=(country==15);
gen italy=(country==16);
gen france=(country==17);
gen denmark=(country==18);
gen greece=(country==19);
gen switz=(country==20);
gen belgium=(country==23);

gen group=1 if (country==13 | country==14 | country==18);
replace group=2 if (country==11 | country==12 | country==17 | country==20 | country==23);
replace group=3 if (country==15 | country==16 | country==19);

gen dfcare=((nurs==1) | (nursm==1) | (homca1==1 | homca2==1 | homca3==1) | (homca1m==1 | homca2m==1 | homca3m==1) | cv178_==2);
gen dfcare_priv=(hc039d9== 1 | hc039d9m==1 | hc039d10== 1 | hc039d10m==1 | hc039d11== 1 | hc039d11m==1); 
tab dfcare group if ivphealth==1, col;
tab dfcare group if adlgd3==1, col;
tab dfcare group if adlgd2==1, col;
tab dfcare group if mobilitygd2==1, col;
tab dfcare group if mobilitygd==1, col;
gen care=(daily==1 | dfcare==1);
tab dfcare group if care==1, col;

**I contruct a variable that tells me if there is at least one parent who is receiving care (all types);
gen care_all_w=((sp002==1 & ((sp005_1==1 | sp005_1==2) | (sp005_2==1 | sp005_2==2) | (sp005_3==1 | sp005_3==2)))  | sp020_==1 | sp020m==1 | dfcare==1);
gen care_all_d=((sp002==1 & ((sp005_1==1) | (sp005_2==1) | (sp005_3==1)))  | sp020_==1 | sp020m==1 | dfcare==1);


sum worker1 carer daily atweek1 ivphealth phealthvg phealthg phealthf phealthb adlgd adlgd2 adlgd3 mobilitygd limitedgd  if group==1;
sum worker1 carer daily atweek1 ivphealth phealthvg phealthg phealthf phealthb adlgd adlgd2 adlgd3 mobilitygd limitedgd if group==2;
sum worker1 carer daily atweek1 ivphealth phealthvg phealthg phealthf phealthb adlgd adlgd2 adlgd3 mobilitygd limitedgd if group==3;


sum worker1 carer daily atweek1 ivphealth age1 phealthvg phealthg phealthf phealthb adlgd adlgd2 adlgd3 mobilitygd limitedgd  if group==1;
sum worker1 carer daily atweek1 ivphealth age1 phealthvg phealthg phealthf phealthb adlgd adlgd2 adlgd3 mobilitygd limitedgd if group==2;
sum worker1 carer daily atweek1 ivphealth age1 phealthvg phealthg phealthf phealthb adlgd adlgd2 adlgd3 mobilitygd limitedgd if group==3;

sum atweek1 if group==1 & carer==1;
sum atweek1 if group==2 & carer==1;
sum atweek1 if group==3 & carer==1;

sum daily if group==1 & carer==1;
sum daily if group==2 & carer==1;
sum daily if group==3 & carer==1;


 **Dummy for education;
gen educ=(educ1==1 | educ2==1);

       

**Different vectors of instruments;
gen instmental=(mentald==1);
**Katz Index;
**This is combining PH049 and PH=10 (continence) in adlg_k2;
gen katz2=6 if adlg_k2==0;
replace katz2=5 if adlg_k2==1;
replace katz2=4 if adlg_k2==2;
replace katz2=3 if adlg_k2==3;
replace katz2=2 if adlg_k2==4;
replace katz2=1 if adlg_k2==5;
replace katz2=0 if adlg_k2==6;
gen katz2_6=(katz2==6);
gen katz2_4=(katz2==4);
gen katz2_2=(katz2<=2);
gen katz2_0=(katz2==0);


**This is the definition (katz4_low, katz4_med) used in the table 6 of the paper;
gen katz4_6=(katz2==6);
gen katz4_low=(katz2<=2);
gen katz4_med=(katz2==3 | katz2==4 | katz2==5 );


**Different definitions of good parental health;
gen inst000k_2=(ivphealth==0 & katz4_low==0 & katz4_med==0 & mentald==0); **Perfect health based on the first 4 instruments;
gen inst000k_3=(ivphealth==0 & katz2_6==1 & mentald==0 & mobilitygd==0); **Perfect health based on the 5 instruments;

**Different combinations of parental disability conditions;
gen katz_low=(katz4_low==1 & katz4_med==0 & mentald==0);
gen katz_med=(ivphealth==0 & katz4_low==0 & katz4_med==1 & mentald==0);
gen instpoork4=(ivphealth==1 &  katz4_low==0 & mentald==0);
gen instotherk4=(ivphealth==0 & katz4_low==0 & katz4_med==0 & mentald==0 & mobilitygd==1); 

sum ivphealth inst000k_2 inst000k_3 katz_low katz_med instpoork4 mentald instotherk4 if group==1;
sum ivphealth inst000k_2 inst000k_3 katz_low katz_med instpoork4 mentald instotherk4 if group==2;
sum ivphealth inst000k_2 inst000k_3 katz_low katz_med instpoork4 mentald instotherk4 if group==3;


**RESULTS: From the following regressions, we obtain results in Table 6. We need to activate the proper regression and predictions each time to generate the corresponding
result in each row of the table;

**northern countries;
**This is to generate result in Panel A row 1;
*biprobit (worker1=ivphealth age1 educ sisters) (daily=ivphealth age1 educ sisters) if group==1, cluster(id1); 
**This is to generate result in Panel A row 2;
*biprobit (worker1=inst000k_2 age1 educ sisters) (daily=inst000k_2 age1 educ sisters) if group==1, cluster(id1);
**This is to generate result in Panel A row 3;
*biprobit (worker1=inst000k_3 age1 educ sisters) (daily=inst000k_3 age1 educ sisters) if group==1, cluster(id1);


**This is to generate result in Panel B row 1;
*biprobit (worker1=inst000k_3 katz_low age1 educ sisters) (daily=inst000k_3 katz_low age1 educ sisters) if group==1, cluster(id1);
**This is to generate result in Panel B row 2;
*biprobit (worker1=inst000k_3 katz_med age1 educ sisters) (daily=inst000k_3 katz_med age1 educ sisters) if group==1, cluster(id1);
**This is to generate result in Panel B row 3;
*biprobit (worker1=inst000k_3 instpoork4 age1 educ sisters) (daily=inst000k_3 instpoork4 age1 educ sisters) if group==1, cluster(id1);
**This is to generate result in Panel B row 4;
*biprobit (worker1=inst000k_3 mentald age1 educ sisters) (daily=inst000k_3 mentald age1 educ sisters) if group==1, cluster(id1);
**This is to generate result in Panel B row 5;
*biprobit (worker1=inst000k_3 instotherk4 age1 educ sisters) (daily=inst000k_3 instotherk4 age1 educ sisters) if group==1, cluster(id1);
**This is to generate result in Panel B row 6;
biprobit (worker1=inst000k_3 age1 educ sisters) (daily=inst000k_3 age1 educ sisters) if group==1, cluster(id1); 

predict xbhat, xb1;
predict xghat, xb2;
matrix coef=e(b);
svmat coef, name(b);
replace b1=b1[_n-1] in 2/l;
replace b2=b2[_n-1] in 2/l;
replace b6=b6[_n-1] in 2/l;

replace b7=b7[_n-1] in 2/l;
replace b8=b8[_n-1] in 2/l;
*replace b15=b15[_n-1] in 2/l;

**Predictions for the different combinations of instruments;

**This is to generate result in Panel A row 1;
*gen x0b=xbhat-(b1*ivphealth) if group==1;
*gen x1b=x0b+b1 if group==1;
*gen x0g=xghat-(b6*ivphealth) if group==1;
*gen x1g=x0g+b6 if group==1;

**This is to generate result in Panel A row 2;
*gen x0b=xbhat-(b1*inst000k_2)+b1 if group==1; *here we use the dummy for PH=0 in order to avoid the problem of lack of variability in daily when the dummy for PH=1 is zero (lack of
always takers in the case of 4 instruments) and Stata tries to make the constant -infinity to make the probability of daily=1 very low;
*gen x1b=xbhat-(b1*inst000k_2) if group==1;
*gen x0g=xghat-(b6*inst000k_2)+b6 if group==1;
*gen x1g=xghat-(b6*inst000k_2) if group==1;

**This is to generate result in Panel A row 3 and in Panel B row 6;
gen x0b=xbhat-(b1*inst000k_3)+b1 if group==1; *here we use the dummy for PH=0 in order to avoid the problem of lack of variability in daily when the dummy for PH=1 is zero (lack of
always takers in the case of 4 instruments) and Stata tries to make the constant -infinity to make the probability of daily=1 very low;
gen x1b=xbhat-(b1*inst000k_3) if group==1;
gen x0g=xghat-(b6*inst000k_3)+b6 if group==1;
gen x1g=xghat-(b6*inst000k_3) if group==1;

**This is to generate result in Panel B row 1;
*gen x0b=xbhat-(b1*inst000k_3)-(b2*katz_low)+b1 if group==1;
*gen x1b=xbhat-(b1*inst000k_3)-(b2*katz_low)+b2 if group==1;
*gen x0g=xghat-(b7*inst000k_3)-(b8*katz_low)+b7 if group==1;
*gen x1g=xghat-(b7*inst000k_3)-(b8*katz_low)+b8 if group==1;

**This is to generate result in Panel B row 2;
*gen x0b=xbhat-(b1*inst000k_3)-(b2*katz_med)+b1 if group==1;
*gen x1b=xbhat-(b1*inst000k_3)-(b2*katz_med)+b2 if group==1;
*gen x0g=xghat-(b7*inst000k_3)-(b8*katz_med)+b7 if group==1;
*gen x1g=xghat-(b7*inst000k_3)-(b8*katz_med)+b8 if group==1;

**This is to generate result in Panel B row 3;
*gen x0b=xbhat-(b1*inst000k_3)-(b2*instpoork4)+b1 if group==1;
*gen x1b=xbhat-(b1*inst000k_3)-(b2*instpoork4)+b2 if group==1;
*gen x0g=xghat-(b7*inst000k_3)-(b8*instpoork4)+b7 if group==1;
*gen x1g=xghat-(b7*inst000k_3)-(b8*instpoork4)+b8 if group==1;

**This is to generate result in Panel B row 4;
*gen x0b=xbhat-(b1*inst000k_3)-(b2*mentald)+b1 if group==1;
*gen x1b=xbhat-(b1*inst000k_3)-(b2*mentald)+b2 if group==1;
*gen x0g=xghat-(b7*inst000k_3)-(b8*mentald)+b7 if group==1;
*gen x1g=xghat-(b7*inst000k_3)-(b8*mentald)+b8 if group==1;

**This is to generate result in Panel B row 5;
*gen x0b=xbhat-(b1*inst000k_3)-(b2*instotherk4)+b1 if group==1;
*gen x1b=xbhat-(b1*inst000k_3)-(b2*instotherk4)+b2 if group==1;
*gen x0g=xghat-(b7*inst000k_3)-(b8*instotherk4)+b7 if group==1;
*gen x1g=xghat-(b7*inst000k_3)-(b8*instotherk4)+b8 if group==1;


gen df0b=normal(x0b) if group==1;
gen df1b=normal(x1b) if group==1;
gen df0g=normal(x0g) if group==1;
gen df1g=normal(x1g) if group==1;
gen num=df1b-df0b if group==1;
gen denom=df1g-df0g if group==1;
gen at=df0g if group==1;
gen nt=1-df1g if group==1;

drop xbhat xghat b1-b11;


**central countries;
**This is to generate result in Panel A row 1;
*biprobit (worker1=ivphealth age1 educ sisters) (daily=ivphealth age1 educ sisters) if group==2, cluster(id1); 
**This is to generate result in Panel A row 2;
*biprobit (worker1=inst000k_2 age1 educ sisters) (daily=inst000k_2 age1 educ sisters) if group==2, cluster(id1);
**This is to generate result in Panel A row 3;
*biprobit (worker1=inst000k_3 age1 educ sisters) (daily=inst000k_3 age1 educ sisters) if group==2, cluster(id1);


**This is to generate result in Panel B row 1;
*biprobit (worker1=inst000k_3 katz_low age1 educ sisters) (daily=inst000k_3 katz_low age1 educ sisters) if group==2, cluster(id1);
**This is to generate result in Panel B row 2;
*biprobit (worker1=inst000k_3 katz_med age1 educ sisters) (daily=inst000k_3 katz_med age1 educ sisters) if group==2, cluster(id1);
**This is to generate result in Panel B row 3;
*biprobit (worker1=inst000k_3 instpoork4 age1 educ sisters) (daily=inst000k_3 instpoork4 age1 educ sisters) if group==2, cluster(id1);
**This is to generate result in Panel B row 4;
*biprobit (worker1=inst000k_3 mentald age1 educ sisters) (daily=inst000k_3 mentald age1 educ sisters) if group==2, cluster(id1);
**This is to generate result in Panel B row 5;
*biprobit (worker1=inst000k_3 instotherk4 age1 educ sisters) (daily=inst000k_3 instotherk4 age1 educ sisters) if group==2, cluster(id1);
**This is to generate result in Panel B row 6;
biprobit (worker1=inst000k_3 age1 educ sisters) (daily=inst000k_3 age1 educ sisters) if group==2, cluster(id1); 

predict xbhat, xb1;
predict xghat, xb2;
matrix coef=e(b);
svmat coef, name(b);
replace b1=b1[_n-1] in 2/l;
replace b2=b2[_n-1] in 2/l;
replace b6=b6[_n-1] in 2/l;

replace b7=b7[_n-1] in 2/l;
replace b8=b8[_n-1] in 2/l;
*replace b15=b15[_n-1] in 2/l;

**Predictions for the different combinations of instruments;

**This is to generate result in Panel A row 1;
*replace x0b=xbhat-(b1*ivphealth) if group==2;
*replace x1b=x0b+b1 if group==2;
*replace x0g=xghat-(b6*ivphealth) if group==2;
*replace x1g=x0g+b6 if group==2;

**This is to generate result in Panel A row 2;
*replace x0b=xbhat-(b1*inst000k_2)+b1 if group==2; *here we use the dummy for PH=0 in order to avoid the problem of lack of variability in daily when the dummy for PH=1 is zero (lack of
always takers in the case of 4 instruments) and Stata tries to make the constant -infinity to make the probability of daily=1 very low;
*replace x1b=xbhat-(b1*inst000k_2) if group==2;
*replace x0g=xghat-(b6*inst000k_2)+b6 if group==2;
*replace x1g=xghat-(b6*inst000k_2) if group==2;

**This is to generate result in Panel A row 3 and in Panel B row 6;
replace x0b=xbhat-(b1*inst000k_3)+b1 if group==2; *here we use the dummy for PH=0 in order to avoid the problem of lack of variability in daily when the dummy for PH=1 is zero (lack of
always takers in the case of 4 instruments) and Stata tries to make the constant -infinity to make the probability of daily=1 very low;
replace x1b=xbhat-(b1*inst000k_3) if group==2;
replace x0g=xghat-(b6*inst000k_3)+b6 if group==2;
replace x1g=xghat-(b6*inst000k_3) if group==2;

**This is to generate result in Panel B row 1;
*replace x0b=xbhat-(b1*inst000k_3)-(b2*katz_low)+b1 if group==2;
*replace x1b=xbhat-(b1*inst000k_3)-(b2*katz_low)+b2 if group==2;
*replace x0g=xghat-(b7*inst000k_3)-(b8*katz_low)+b7 if group==2;
*replace x1g=xghat-(b7*inst000k_3)-(b8*katz_low)+b8 if group==2;

**This is to generate result in Panel B row 2;
*replace x0b=xbhat-(b1*inst000k_3)-(b2*katz_med)+b1 if group==2;
*replace x1b=xbhat-(b1*inst000k_3)-(b2*katz_med)+b2 if group==2;
*replace x0g=xghat-(b7*inst000k_3)-(b8*katz_med)+b7 if group==2;
*replace x1g=xghat-(b7*inst000k_3)-(b8*katz_med)+b8 if group==2;

**This is to generate result in Panel B row 3;
*replace x0b=xbhat-(b1*inst000k_3)-(b2*instpoork4)+b1 if group==2;
*replace x1b=xbhat-(b1*inst000k_3)-(b2*instpoork4)+b2 if group==2;
*replace x0g=xghat-(b7*inst000k_3)-(b8*instpoork4)+b7 if group==2;
*replace x1g=xghat-(b7*inst000k_3)-(b8*instpoork4)+b8 if group==2;

**This is to generate result in Panel B row 4;
*replace x0b=xbhat-(b1*inst000k_3)-(b2*mentald)+b1 if group==2;
*replace x1b=xbhat-(b1*inst000k_3)-(b2*mentald)+b2 if group==2;
*replace x0g=xghat-(b7*inst000k_3)-(b8*mentald)+b7 if group==2;
*replace x1g=xghat-(b7*inst000k_3)-(b8*mentald)+b8 if group==2;

**This is to generate result in Panel B row 5;
*replace x0b=xbhat-(b1*inst000k_3)-(b2*instotherk4)+b1 if group==2;
*replace x1b=xbhat-(b1*inst000k_3)-(b2*instotherk4)+b2 if group==2;
*replace x0g=xghat-(b7*inst000k_3)-(b8*instotherk4)+b7 if group==2;
*replace x1g=xghat-(b7*inst000k_3)-(b8*instotherk4)+b8 if group==2;

replace df0b=normal(x0b) if group==2;
replace df1b=normal(x1b) if group==2;
replace df0g=normal(x0g) if group==2;
replace df1g=normal(x1g) if group==2;
replace num=df1b-df0b if group==2;
replace denom=df1g-df0g if group==2;
replace at=df0g if group==2;
replace nt=1-df1g if group==2;


drop xbhat xghat b1-b11;


**southern countries;
**This is to generate result in Panel A row 1;
*biprobit (worker1=ivphealth age1 educ sisters) (daily=ivphealth age1 educ sisters) if group==3, cluster(id1); 
**This is to generate result in Panel A row 2;
*biprobit (worker1=inst000k_2 age1 educ sisters) (daily=inst000k_2 age1 educ sisters) if group==3, cluster(id1);
**This is to generate result in Panel A row 3;
*biprobit (worker1=inst000k_3 age1 educ sisters) (daily=inst000k_3 age1 educ sisters) if group==3, cluster(id1);


**This is to generate result in Panel B row 1;
*biprobit (worker1=inst000k_3 katz_low age1 educ sisters) (daily=inst000k_3 katz_low age1 educ sisters) if group==3, cluster(id1);
**This is to generate result in Panel B row 2;
*biprobit (worker1=inst000k_3 katz_med age1 educ sisters) (daily=inst000k_3 katz_med age1 educ sisters) if group==3, cluster(id1);
**This is to generate result in Panel B row 3;
*biprobit (worker1=inst000k_3 instpoork4 age1 educ sisters) (daily=inst000k_3 instpoork4 age1 educ sisters) if group==3, cluster(id1);
**This is to generate result in Panel B row 4;
*biprobit (worker1=inst000k_3 mentald age1 educ sisters) (daily=inst000k_3 mentald age1 educ sisters) if group==3, cluster(id1);
**This is to generate result in Panel B row 5;
*biprobit (worker1=inst000k_3 instotherk4 age1 educ sisters) (daily=inst000k_3 instotherk4 age1 educ sisters) if group==3, cluster(id1);
**This is to generate result in Panel B row 6;
biprobit (worker1=inst000k_3 age1 educ sisters) (daily=inst000k_3 age1 educ sisters) if group==3, cluster(id1); 

predict xbhat, xb1;
predict xghat, xb2;
matrix coef=e(b);
svmat coef, name(b);
replace b1=b1[_n-1] in 2/l;
replace b2=b2[_n-1] in 2/l;
replace b6=b6[_n-1] in 2/l;

replace b7=b7[_n-1] in 2/l;
replace b8=b8[_n-1] in 2/l;
*replace b15=b15[_n-1] in 2/l;

**Predictions for the different combinations of instruments;

**This is to generate result in Panel A row 1;
*replace x0b=xbhat-(b1*ivphealth) if group==3;
*replace x1b=x0b+b1 if group==3;
*replace x0g=xghat-(b6*ivphealth) if group==3;
*replace x1g=x0g+b6 if group==3;

**This is to generate result in Panel A row 2;
*replace x0b=xbhat-(b1*inst000k_2)+b1 if group==3; *here we use the dummy for PH=0 in order to avoid the problem of lack of variability in daily when the dummy for PH=1 is zero (lack of
always takers in the case of 4 instruments) and Stata tries to make the constant -infinity to make the probability of daily=1 very low;
*replace x1b=xbhat-(b1*inst000k_2) if group==3;
*replace x0g=xghat-(b6*inst000k_2)+b6 if group==3;
*replace x1g=xghat-(b6*inst000k_2) if group==3;

**This is to generate result in Panel A row 3 and in Panel B row 6;
replace x0b=xbhat-(b1*inst000k_3)+b1 if group==3; *here we use the dummy for PH=0 in order to avoid the problem of lack of variability in daily when the dummy for PH=1 is zero (lack of
always takers in the case of 4 instruments) and Stata tries to make the constant -infinity to make the probability of daily=1 very low;
replace x1b=xbhat-(b1*inst000k_3) if group==3;
replace x0g=xghat-(b6*inst000k_3)+b6 if group==3;
replace x1g=xghat-(b6*inst000k_3) if group==3;

**This is to generate result in Panel B row 1;
*replace x0b=xbhat-(b1*inst000k_3)-(b2*katz_low)+b1 if group==3;
*replace x1b=xbhat-(b1*inst000k_3)-(b2*katz_low)+b2 if group==3;
*replace x0g=xghat-(b7*inst000k_3)-(b8*katz_low)+b7 if group==3;
*replace x1g=xghat-(b7*inst000k_3)-(b8*katz_low)+b8 if group==3;

**This is to generate result in Panel B row 2;
*replace x0b=xbhat-(b1*inst000k_3)-(b2*katz_med)+b1 if group==3;
*replace x1b=xbhat-(b1*inst000k_3)-(b2*katz_med)+b2 if group==3;
*replace x0g=xghat-(b7*inst000k_3)-(b8*katz_med)+b7 if group==3;
*replace x1g=xghat-(b7*inst000k_3)-(b8*katz_med)+b8 if group==3;

**This is to generate result in Panel B row 3;
*replace x0b=xbhat-(b1*inst000k_3)-(b2*instpoork4)+b1 if group==3;
*replace x1b=xbhat-(b1*inst000k_3)-(b2*instpoork4)+b2 if group==3;
*replace x0g=xghat-(b7*inst000k_3)-(b8*instpoork4)+b7 if group==3;
*replace x1g=xghat-(b7*inst000k_3)-(b8*instpoork4)+b8 if group==3;

**This is to generate result in Panel B row 4;
*replace x0b=xbhat-(b1*inst000k_3)-(b2*mentald)+b1 if group==3;
*replace x1b=xbhat-(b1*inst000k_3)-(b2*mentald)+b2 if group==3;
*replace x0g=xghat-(b7*inst000k_3)-(b8*mentald)+b7 if group==3;
*replace x1g=xghat-(b7*inst000k_3)-(b8*mentald)+b8 if group==3;

**This is to generate result in Panel B row 5;
*replace x0b=xbhat-(b1*inst000k_3)-(b2*instotherk4)+b1 if group==3;
*replace x1b=xbhat-(b1*inst000k_3)-(b2*instotherk4)+b2 if group==3;
*replace x0g=xghat-(b7*inst000k_3)-(b8*instotherk4)+b7 if group==3;
*replace x1g=xghat-(b7*inst000k_3)-(b8*instotherk4)+b8 if group==3;

replace df0b=normal(x0b) if group==3;
replace df1b=normal(x1b) if group==3;
replace df0g=normal(x0g) if group==3;
replace df1g=normal(x1g) if group==3;
replace num=df1b-df0b if group==3;
replace denom=df1g-df0g if group==3;
replace at=df0g if group==3;
replace nt=1-df1g if group==3;


drop xbhat xghat b1-b11;




sort group;
egen numm=mean(num), by (group);
sort group;
egen denomm=mean(denom), by (group);
sort group;
gen latemb=numm/denomm;
sort group;
egen atm=mean(at), by (group);
sort group;
egen ntm=mean(nt), by (group);

**Results in Table 6 (ATs, NTs, Cs, employment effect, caregiving effect, late) for a particular combination of the instruments;
**Employment effect;
tab numm group, col;
**Caregiving effect and mass of compliers;
tab denomm group, col;
**LATE;
tab latemb group, col;
**Mass of always takers;
tab atm group, col;
**Mass of never takers;
tab ntm group, col;
	


save `t1'sampleFRB_w1w2_ext_cross06_p, replace;
keep  worker1 daily ivphealth adlgd2 mentald educ sisters age1 group wave genderh id1 ocare dfcare mobilitygd phealth2 adlg2 adlg_k2;
keep if group==1 & wave==2 & genderh==2;
save `t1'north_w1w2_2006_p, replace;
use `t1'sampleFRB_w1w2_ext_cross06_p;
keep  worker1 daily ivphealth adlgd2 mentald educ sisters age1 group wave genderh id1 ocare dfcare mobilitygd phealth2 adlg2 adlg_k2;
keep if group==2 & wave==2 & genderh==2;
save `t1'cont_w1w2_2006_p, replace;
use `t1'sampleFRB_w1w2_ext_cross06_p;
keep  worker1 daily ivphealth adlgd2 mentald educ sisters age1 group wave genderh id1 ocare dfcare mobilitygd phealth2 adlg2 adlg_k2;
keep if group==3 & wave==2 & genderh==2;
save `t1'sout_w1w2_2006_p, replace;



log close;
